package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "商品基础属性接口")
@RestController
@RequestMapping("admin/product")
public class BaseManageController {
    //  调用服务层接口
    @Autowired
    private ManageService manageService;

    //  获取一级分类数据

    @ApiOperation("获取一级分类数据")
    @GetMapping("getCategory1")
    public Result getCategory(){
        //调用服务层
        List<BaseCategory1> baseCategory1List =  manageService.getCategory1();
        return Result.ok(baseCategory1List);
    }

    // 获取二级分类数据 /admin/product/getCategory2/{category1Id}
    @ApiOperation("获取二级分类数据")
    @GetMapping("getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable Long category1Id){
        //调用服务层
        List<BaseCategory2> baseCategory2List =  manageService.getCategory2(category1Id);
        return Result.ok(baseCategory2List);
    }
    // 获取三级分类数据 /admin/product/getCategory3/{category2Id}
    @ApiOperation("获取三级分类数据")
    @GetMapping("getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable Long category2Id){
        //调用服务层
        List<BaseCategory3> baseCategory3List = manageService.getCategory3(category2Id);
        return Result.ok(baseCategory3List);
    }
    //根据分类Id 获取平台属性集合
    //  /admin/product/attrInfoList/{category1Id}/{category2Id}/{category3Id}
    @ApiOperation("根据分类Id 获取平台属性集合")
    @GetMapping("attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result getAttrInfoList(@PathVariable Long category1Id,
                                  @PathVariable Long category2Id,
                                  @PathVariable Long category3Id){
        List<BaseAttrInfo> baseAttrInfoList = manageService.getAttrInfoList(category1Id,category2Id,category3Id);
        return Result.ok(baseAttrInfoList);
    }

    // Post /admin/product/saveAttrInfo
    //保存平台属性 and 更新平台属性和平台属性值
    @ApiOperation("保存平台属性 and 更新平台属性和平台属性值")
    @PostMapping("saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        manageService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }

    // 根据平台属性Id 获取到平台属性值集合 回显数据到页面
    // GET/admin/product/getAttrValueList/{attrId}
    @ApiOperation("根据平台属性Id 获取到平台属性值集合 回显数据到页面")
    @GetMapping("getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable Long attrId){
        //List<BaseAttrValue> baseAttrValueList = manageService.getAttrValueList(attrId);
        //return Result.ok(baseAttrValueList);

        //  获取数据 ： 本质直接获取这个表中的数据！
        //  select * from base_attr_value where attr_id = attrId;
        //  为了更加符合业务逻辑：可以从 平台属性 中获取平 台属性值 集合！
        //  select * from base_attr_info where id = attrId;

        BaseAttrInfo baseAttrInfo = manageService.getBaseAttrInfo(attrId);
        return Result.ok(baseAttrInfo.getAttrValueList());

    }
}
